<?php
namespace app\index\controller;
use app\index\model\Waybill;
use app\index\model\WaybillHistory;
use app\index\model\WaybillImage;
use think\Db;
use think\response\Json;

class LogisticsController extends BaseController
{
    public function getWaybillParam(): Json
    {
        $page = input('post.pageIndex', 1);
        $size = input('post.pageSize', 10);
        $post = input('post.', [], 'trim');
        $search_param=[
            'ship_no'=>$post['ship_no']??null,
            'status'=>$post['status']??null,
            'customer_name'=>$post['customer_name']??null,
            'search_key'=>$post['search_key']??null,
            'search_value'=>$post['search_value']??null,
            'create_group'=>$post['create_group']??null,
        ];
        $page_param=[
            'page_num'=>$page,
            'pageSize'=>$size,
        ];
        $result=Waybill::getWaybillList($search_param,$page_param);
        return json(['code' => 0, 'waybillList' => $result['waybillList'], 'pageTotal' => $result['total'],'group'=>$result['group']]);
    }

    public function sendGoods()
    {
        $data=input('post.');
        ['status' => $status, 'result' => $result]=Waybill::sendGoods($data);
        if ($status===false){
            return json(['code'=>1,'msg'=>$result]);
        }
        return json(['code'=>0,'msg'=>$result]);
    }

    public function getLogisticsWaybillList()
    {
        $data=input('post.');
        $search_param=[
            'ship_no'=>$data['ship_no']??null,
            'logistics_id'=>$data['logistics_id']??null,
            'logistics_waybill'=>$data['logistics_waybill']??null,
            'customer_name'=>$data['customer_name']??null,
            'search_key'=>$data['search_key']??null,
            'search_value'=>$data['search_value']??null,
            'status'=>$data['status']??null,
        ];
        $page_param=[
            'page_num'=>$data['pageIndex'],
            'pageSize'=>$data['pageSize'],
        ];
        $result=Waybill::getLogisticsWaybillList($search_param,$page_param);
        $logisticsList=Db::name('logistics')->where('del_flag',0)
            ->field('company label,id value,type')->select()->toArray();
        return json(['code' => 0, 'list' => $result['list'],'logisticsList'=>$logisticsList , 'pageTotal' => $result['total']]);
    }

    public function logisticsWaybillEdit()
    {
        $data=input('post.');
        if (!$data['real_weight']){
            return json(['code'=>1,'msg'=>'请输入收费重量']);
        }
        if(!$data['logistics_waybill']){
            return json(['code'=>1,'msg'=>'请输入运单号']);
        }
        ['status'=>$status,'result'=>$result]=Waybill::logisticsWaybillEdit($data);
        if ($status==false){
            return json(['code'=>1,'msg'=>$result]);
        }
        return json(['code'=>0,'msg'=>$result]);
    }
    /*没用*/
    public function logisticsWaybillSend()
    {
        $id=input('post.id');
        $row=Db::name('waybill')->where('id',$id)->find();
        if (!$row['logistics_waybill']||!$row['logistics_id']||!$row['real_weight']){
            return json(['code'=>1,'msg'=>'请完善信息,再发货']);
        }
        ['status'=>$status,'result'=>$result]=Waybill::logisticsWaybillSend($id);
        if ($status==false){
            return json(['code'=>1,'msg'=>$result]);
        }
        return json(['code'=>0,'msg'=>$result]);
    }

    public function getHistory()
    {
        $id=input('post.id');
        $list=WaybillHistory::getHistoryListById($id);
        return json(['code'=>0,'data'=>$list,'pageTotal'=>count($list)]);
    }

    /**
     * addRemark
     * 添加备注
     * @url
     * @method
     * @return Json
     * @author Asira
     * @date 2021/3/26
     */
    public function addRemark(): Json
    {
        $id=input('post.id');
        $remark=input('post.remark');
        $logistics_remark=input('post.logistics_remark');

        if($remark){
            $data=['remark'=>$remark];
        }else if($logistics_remark){
            $data=['logistics_remark'=>$logistics_remark];
        }else{
            return json(['code'=>1,'msg'=>'输入备注']);
        }

        ['status'=>$status,'result'=>$result]=Waybill::addRemark($id,$data);
        if ($status==false){
            return json(['code'=>1,'msg'=>$result]);
        }
        return json(['code'=>0,'msg'=>$result]);
    }

    public function mergeWaybill()
    {
        $data=input('post.data/a');
        $id=[];
        if (count($data)<2){
            return json(['code'=>1,'msg'=>'请选择多个运单']);
        }
        $ids=[];
        foreach($data as $k=>$v){
            $id[] = $v;
            if ($data[0]!==$v){
                $ids[] = $v;
            }
        }
        ['status'=>$status,'result'=>$result]=Waybill::mergeWaybill($data,$id,$ids);
        if ($status==false){
            return json(['code'=>1,'msg'=>$result]);
        }
        return json(['code'=>0,'msg'=>$result]);
    }

    public function getLogistics()
    {
        $id=input('post.id');
        $type=Db::name('logistics')->where('id',$id)->column('type');
        return json(['code'=>0,'type'=>$type[0]]);
    }

    public function getLogisticsById()
    {
        $id=input('post.id');
        $data=Waybill::getLogisticsById($id);
        return json(['code'=>0,'data'=>$data]);
    }

    public function imageChange(){
        $data=input('post.');
        if(empty($data['image'])){
            return json(['code'=>1,'msg'=>'图片不能为空']);
        }
        ['status'=>$status,'result'=>$result]=Waybill::changeImage($data);
        if($status===false){
            return json(['code'=>1,'msg'=>$result]);
        }
        return json(['code'=>0,'msg'=>$result]);
    }

    public function getImageBYId(){
        $id=input('post.id');
        $image_list=WaybillImage::getImageById($id);
        return json(['code'=>0,'image'=>$image_list]);
    }
}